from django.db import models
from django.db.models import signals
from django.contrib.auth.models import User

from events.models import Milestone, timeline_updater
from projects.models import Project

__all__ = ('Issue', 'Urgency', 'Importance', 'STATUS_CHOICES',)

STATUS_CHOICES = (
    ('new', 'New'),
    ('wontfix', "Won't Fix"),
    ('duplicate', 'Duplicate'),
    ('accepted', 'Accepted'),
    ('inprogress', 'In Progress'),
    ('resolved', 'Resolved'),
    ('review', 'Awaiting Review'),
)

ATTACHMENT_STATUS_CHOICES= (
    ('current', 'Current'),
    ('superseded', 'Superseded'),
    ('irrelevant', 'No Longer Relevant'),
)

class Urgency(models.Model):
    title = models.CharField(max_length=35, unique=True)
    order = models.IntegerField(default=1)
    description = models.TextField(blank=True)

    class Meta:
        verbose_name_plural = u"Levels of Urgency"
        verbose_name = u"Level of Urgency"
        ordering = ['order', 'title']

    def __unicode__(self):
        return self.title

    @models.permalink
    def get_absolute_url(self):
        return ('issues.views.urgency', [str(self.id)])

class Importance(models.Model):
    title = models.CharField(max_length=35, unique=True)
    order = models.IntegerField(default=1)
    description = models.TextField(blank=True)

    class Meta:
        verbose_name_plural = u"Levels of Importance"
        verbose_name = u"Level of Importance"
        ordering = ['order', 'title']

    def __unicode__(self):
        return self.title

    @models.permalink
    def get_absolute_url(self):
        return ('issues.views.importance', [str(self.id)])

class Issue(models.Model):
    project = models.ForeignKey(Project, related_name='issues', editable=False)
    summary = models.CharField(max_length=255)
    description = models.TextField()
    date_added = models.DateTimeField(auto_now_add=True)
    date_modified = models.DateTimeField(auto_now=True)
    date_due = models.DateTimeField(null=True, blank=True)
    milestone = models.ForeignKey(Milestone, null=True, blank=True)
    creator = models.ForeignKey(User, related_name="created_issues", editable=False)
    owners = models.ManyToManyField(User, related_name="assigned_issues", null=True, blank=True)
    status = models.CharField(max_length=10, choices=STATUS_CHOICES, default='new')
    order = models.IntegerField(blank=True, null=True)

    class Meta:
        ordering = ['-date_due', 'id']

    def __unicode__(self):
        return u'#%d %s' % (self.id, self.summary)

    @models.permalink
    def get_absolute_url(self):
        return ('issue_detail', [self.project.slug, str(self.id)])

class Attachment(models.Model):
    creator = models.ForeignKey(User, related_name="created_issue_attachments")
    date_added = models.DateTimeField(auto_now_add=True)
    status = models.CharField(max_length=10, choices=ATTACHMENT_STATUS_CHOICES, default='current')
    attachment = models.FileField(upload_to='attachments/%Y/%m/%d')
    issue = models.ForeignKey(Issue, related_name="attachments")

    def __unicode__(self):
        return u'Attachment: %s' % self.attachment.name

    @models.permalink
    def get_absolute_url(self):
        return ('issues.views.attachment', [str(self.id)])

# Signals
signals.post_save.connect(timeline_updater, sender=Issue)
